﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using League.Entity;
using System.IO;
using JJY.CommonHelper;
using System.Runtime.Serialization.Json;
using System.Web.Script.Serialization;
using HtmlAgilityPack;
using League.DataAccess;

namespace League.Business
{
    public class TeamMatchDataOrganizeMonitor
    {
        JavaScriptSerializer Serializer = new JavaScriptSerializer();

        private LeagueMatch leagueMatch = new LeagueMatch();
        private LeagueTeamData homeTeamData = new LeagueTeamData();
        private LeagueTeamData visitingTeamData = new LeagueTeamData();
        private LeagueInfo leagueInfo = new LeagueInfo();
        private LeagueMatchStatsHtml leagueMatchStat = new LeagueMatchStatsHtml();

        public LeagueMatch _LeagueMatch
        {
            get { return leagueMatch; }
        }

        public LeagueTeamData _HomeTeamData
        {
            get { return homeTeamData; }
        }

        public LeagueTeamData _VisitingTeamData
        {
            get { return visitingTeamData; }
        }

        public LeagueInfo _LeagueInfo
        {
            get { return leagueInfo; }
        }

        public LeagueMatchStatsHtml _LeagueMatchStat
        {
            get { return leagueMatchStat; }
        }

        public TeamMatchDataOrganizeMonitor(string statsUrl, string jsonUrl, string strJson)
        {
            List<TeamDataFromJson> teamDataFromJson = Serializer.Deserialize<List<TeamDataFromJson>>(strJson);
            homeTeamData = Transfer(teamDataFromJson[0].home);
            visitingTeamData = Transfer(teamDataFromJson[0].away);

            leagueInfo.LeagueName = teamDataFromJson[0].home.competitionName;
            leagueInfo.SeasonYear = homeTeamData.SeasonYear;

            //leagueMatch.SeasonYear = homeTeam.SeasonYear;
            leagueMatch.RoundNumber = homeTeamData.RoundNumber;

            leagueMatchStat.LeagueName = leagueInfo.LeagueName;
            leagueMatchStat.SeasonYear = leagueInfo.SeasonYear;
            leagueMatchStat.RoundNumber = leagueMatch.RoundNumber;
            leagueMatchStat.HomeTeam = homeTeamData.TeamName;
            leagueMatchStat.VisitingTeam = visitingTeamData.TeamName;
            leagueMatchStat.MatchStatsUrl = statsUrl;
            leagueMatchStat.MatchStatsHtml = HttpRequestService.GetHtmlSourceByUrl(statsUrl, Encoding.UTF8);
            leagueMatchStat.MatchStatsJsonUrl = jsonUrl;
            leagueMatchStat.MatchStatsJson = strJson;
            leagueMatchStat.CreateTime = System.DateTime.Now;
        }

        private LeagueTeamData Transfer(TeamData teamData)
        {
            LeagueTeamData result = new LeagueTeamData();
            result.SeasonYear = short.Parse(teamData.seasonId);
            result.RoundNumber = byte.Parse(teamData.matchDay);
            result.TeamName = teamData.team;
            result.Goal = teamData.goals;
            result.ShotOnTarget = teamData.ontargetScoringAtt;
            result.Shot = teamData.totalScoringAtt;
            result.BlockedShot = (byte)((int)teamData.savedIbox + (int)teamData.savedObox);
            result.Corner = teamData.wonCorners;
            result.FreeKick = teamData.fkFoulLost;
            result.OutOfBounds = teamData.totalThrows;
            result.Foul = teamData.fouls;
            result.Offside = teamData.totalOffside;
            result.BallControl = teamData.possessionPercentage;
            result.PassSuccess = teamData.passPercent;
            result.PassMiddleSuccess = teamData.crossPercent;
            result.TacklingSuccess = teamData.tacklePercent;
            result.HeadSuccess = teamData.aerialPercent;
            result.YellowCard = teamData.totalYelCard;
            result.RedCard = teamData.totalRedCard;
            return result;
        }
    }
}
